package org.karen.droid.logviewtools.view.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.karen.droid.logviewtools.R;
import org.karen.droid.logviewtools.application.LGApplication;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    private static final String TAG = "MainActivity";

    private LGApplication application = null;

    private Button mBackBtn = null;

    HorizontalScrollView mHorScrollView = null;

    private ListView mFolderListView = null;
    private ListView mSubFileListView = null;
    private List<File> mFolderFile = null;
    private List<File> mSubFolderFile = null;

    private BlockingQueue<List<File>> mFolderBackStackQueue = null;
    private BlockingQueue<List<File>> mSubFolderBackStackQueue = null;

    private ListViewAdapter mFolderAdapter = null;
    private ListViewAdapter mSubAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main2);
        application = (LGApplication) getApplication();
        builViews();
        initData();
    }

    private void builViews() {
        Log.d(TAG, "builViews");
        mBackBtn = (Button) findViewById(R.id.btn_back_id);
        mBackBtn.setOnClickListener(this);

        mFolderListView = (ListView) findViewById(R.id.listview_folder_id);
        mFolderListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                File f = (File) view.getTag(R.layout.item_listview_file);
                if (null == f) {
                    return;
                }
                mSubFolderFile = application.getFils(f);
                mSubAdapter.setData(mSubFolderFile);
            }
        });

        mSubFileListView = (ListView) findViewById(R.id.listview_file_id);
        mSubFileListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object obj = view.getTag(R.layout.item_listview_file);
                File mFile = null;
                if (null == obj) {
                    return;
                }
                mFile = (File) obj;
                onSubFileClick(mFile);
            }
        });
    }

    private void initData() {
        Log.d(TAG, "initData");
        mFolderBackStackQueue = new LinkedBlockingQueue<List<File>>();
        mSubFolderBackStackQueue = new LinkedBlockingQueue<List<File>>();

        mFolderFile = application.getFils(application.getSDCardRoot());
        mFolderAdapter = new ListViewAdapter(mFolderFile);
        mFolderListView.setAdapter(mFolderAdapter);

        mSubAdapter = new ListViewAdapter(mSubFolderFile);
        mSubFileListView.setAdapter(mSubAdapter);
    }

    private void onSubFileClick(File mFile) {
        if (null == mFile) {
            return;
        }

        if (mFile.isDirectory()) {
            showDirectory(mFile);
        }
    }

    private void showDirectory(File mFile) {
        mFolderBackStackQueue.add(mFolderFile);
        mSubFolderBackStackQueue.add(mSubFolderFile);
        mFolderFile = mSubFolderFile;
        mFolderAdapter.setData(mFolderFile);

        mSubFolderFile = application.getFils(mFile);
        mSubAdapter.setData(mSubFolderFile);
    }

    private void showLogFils(File mFile) {
        Intent mIntent = new Intent();
        mIntent.setClass(getApplicationContext(), ShowLogActivity.class);
        mIntent.putExtra("file_path", mFile.getAbsolutePath());
        startActivity(mIntent);
    }

    /*
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_loadfiles_id: {
            Intent mIntent = new Intent();
            mIntent.setClass(getApplicationContext(), ShowLogActivity.class);
            startActivity(mIntent);
            break;
        }
        case R.id.btn_back_id: {
            if(mFolderBackStackQueue.size() > 0){
                
            }
            break;
        }
        default:
            break;
        }
    }

    private class ListViewAdapter extends BaseAdapter {
        private List<File> mList = null;

        public ListViewAdapter(List<File> mList) {
            this.mList = mList;
        }

        public void setData(List<File> mList) {
            this.mList = mList;
            this.notifyDataSetChanged();
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return (null == mList) ? 0 : mList.size();
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            return null;
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            return 0;
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final File bean = mList.get(position);
            if (null == bean) {
                return null;
            }

            final ViewHolder mViewHolder;
            if (null == convertView) {
                mViewHolder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.item_listview_file, null);
                mViewHolder.fileName = (TextView) convertView.findViewById(R.id.text_filename_id);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(R.layout.item_listview_file, bean);
            mViewHolder.fileName.setText(bean.getName());
            return convertView;
        }

        private class ViewHolder {
            public TextView fileName;
        }

    }

}
